package db_main

import (
	"fmt"
	"gorm.io/gorm"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetUserProfileParam(param map[string]interface{}) (*models_main.UserProfile, error) {
	fName := "GetUserProfileParam"
	o := &models_main.UserProfile{}
	table := mysql.GetRTable(&models_main.UserProfile{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetUserProfilesParam(param map[string]interface{}) ([]*models_main.UserProfile, error) {
	o := make([]*models_main.UserProfile, 0)
	err := mysql.Condition(mysql.GetRTable(&models_main.UserProfile{}), param, "", "", 0, 0).Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	return o, err
}

func UpdateProfileByTransactions(session *gorm.DB, columns []string, profile *models_main.UserProfile) error {
	err := session.Select(columns).Updates(profile).Error
	if err != nil {
		session.Rollback()
		return fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	return nil
}

func InsertUserProfileByTransactions(tx *gorm.DB, profile *models_main.UserProfile) error {
	err := tx.Create(profile).Error
	if err != nil {
		tx.Rollback()
		return fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	return nil
}
